package com.miplus.ccrm.core.security;

import com.miplus.ccrm.common.EnumDataStatus;
import com.miplus.ccrm.common.JDataService;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.JSysUser;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Component
public class JCommonData {
    @Resource
    private JDataService dataService;

    public String getCurrUsrID()
    {
        HttpServletRequest h=((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String token= JPubUtils.getToken(h);
        String key = JPubUtils.getTokenKey(token);
        JSysUser user = (JSysUser) dataService.get(key);
        return user.getUsrid();
    }

    public JSysUser getCurrUsr()
    {
        HttpServletRequest h=((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String token= JPubUtils.getToken(h);
        String key = JPubUtils.getTokenKey(token);
        return (JSysUser) dataService.get(key);
    }

    public String getDistQuyKey(String usrid)
    {
        return "distcrdcasequyparas_"+usrid;
    }

    public String getRecyQuyKey(String usrid)
    {//2024-07-28
        return "recycrdcasequyparas_"+usrid;
    }

    public String getAssQuyKey(String usrid)
    {
        return "asscrdcasequyparas_"+usrid;
    }

    public String getDistResultKey(String usrid)
    {
        return "distcrdcaseresult_"+usrid;
    }

    public String getAssResultKey(String usrid)
    {
        return "asscrdcaseresult_"+usrid;
    }

    public String getQuyNextCaseKey(String usrid)
    {//2024-10-30
        return "nextcasequyparas_"+usrid;
    }

    public String getQuyNextCaseDataKey(String usrid)
    {//2024-10-30
        return "nextcasequydata_"+usrid;
    }

    public boolean setData(String key,Object d,long time)
    {
        return dataService.set(key,d,time);
    }

    public Object getData(String key)
    {
        if(dataService.hasKey(key)) {
            return dataService.get(key);
        } return null;
    }

    public void delData(String key)
    {
        if(dataService.hasKey(key)) {
            dataService.del(key);
        }
    }

    public void SetUsrSmsVCode(String usrid,String code)
    {
        String keyid=JPubUtils.getVCodeIden(usrid);
        dataService.set(keyid,code,JPubUtils.vcodeExpiration);
    }


    //当前正在进线批量回收处理中..
    public boolean IsRecycleProcess()
    {
        String key=JPubUtils.recvProcessKey;
        if(dataService.hasKey(key)) {
            Integer count=(Integer)dataService.get(key);
            if(count!=null) return true;
        }
        return false;
    }

    public void SetRecycleProcess(Integer count)
    {
        String key=JPubUtils.recvProcessKey;
        dataService.set(key,count,1800);
    }

    public Integer GetRecycleProcessCount()
    {
        Integer count=-1;
        String key=JPubUtils.recvProcessKey;
        if(dataService.hasKey(key)) {
            count = (Integer) dataService.get(key);
        }
        return count;
    }

    public void RemoveRecycleProcess()
    {
        String key=JPubUtils.recvProcessKey;
        delData(key);
    }

    public EnumDataStatus getCommissionStatus(String batchid, String grpid)
    {
        String key=batchid+"_"+grpid+"_"+"cmsreportstatus";
        Object ret=getData(key);
        if(ret==null){
            return EnumDataStatus.nready;
        }
        if(JPubUtils.IsEqual(ret.toString(),EnumDataStatus.readying.name)){
            return EnumDataStatus.readying;
        }
        return EnumDataStatus.ready;
    }

    public boolean setCommissionStatus(String batchid,String grpid,EnumDataStatus status,long time)
    {
        String key=batchid+"_"+grpid+"_"+"cmsreportstatus";
        return setData(key,status.name,time);
    }

    public void clearCommissionStatus(String batchid,String grpid)
    {
        String key=batchid+"_"+grpid+"_"+"cmsreportstatus";
        delData(key);
    }
}
